/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.DataObjectEntity;
import entity.ReservationEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author wei
 */
@Stateless
public class ReservationEntityFacade extends AbstractFacade<ReservationEntity> {
    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ReservationEntityFacade() {
        super(ReservationEntity.class);
    }

    public DataModel findSOReservations(Long salesOrderID) {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<DataObjectEntity> reservationRoot = cq.from(ReservationEntity.class);

        Predicate predicate1 = builder.equal(reservationRoot.get("salesOrder").get("id"), salesOrderID);
        Predicate predicate2 = builder.equal(reservationRoot.get("archived"), false);
        Predicate predicate3 = builder.equal(reservationRoot.get("planned"), false);

        cq.select(reservationRoot);
        cq.where(builder.and(predicate1, predicate2, predicate3));

        return new ListDataModel(getEntityManager().createQuery(cq).getResultList());
    }

    public List findReservation() {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<DataObjectEntity> reservationRoot = cq.from(ReservationEntity.class);

        Predicate predicate1 = builder.equal(reservationRoot.get("planned"), false);
        Predicate predicate2 = builder.equal(reservationRoot.get("archived"), false);

        cq.select(reservationRoot);
        cq.where(builder.and(predicate1, predicate2));
        return new ArrayList(getEntityManager().createQuery(cq).getResultList());
    }
}
